<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h3 id="user" style="color:#ff0000">点击获取用户列表</h3>
<div id="user-list">


</div>
<h3 id="system" style="color:#a0d563">点击获取系统信息</h3>
<div id="system-info">


</div>
<h3 id="job" style="color:#5fd4d7">获取任务列表</h3>
<div id="system-job">


</div>
<h3 id="insert-job" style="color:#5fb5d7">插入一条定时任务</h3>
<h3 id="change-job1" style="color:#e8cd6b">改变jobId=1的任务状态</h3>
<h3 id="export-job" style="color:#884cbb">导出定时任务</h3>
<h3 id="gencode" style="color:#3def29">生成代码</h3>
<script src="/js/cookies.js" type="text/javascript"></script>
<script src="/js/jquery.js" type="text/javascript"></script>
<script src="/js/layer/layer.js" type="text/javascript"></script>
<script type="text/javascript">
    //插入job的数据
    var insertJobData = {
        jobId: 1,
        jobName: '系统默认（有参）',
        jobGroup: 'DEFAULT',
        invokeTarget: "myTask.params('hello quartz')",
        cronExpression: '0/2 * * * * ?',
        concurrent: 1
    }
    //改变job状态的变量
    var changeJobData = {}
    //生成代码，zip二进制等文件需要原生的XMLHttpRequest才行，不然解压出来的zip文件格式是错误的
    $("#gencode").click(function () {
        var xhr = new XMLHttpRequest();
        xhr.open('get', '/tool/gen/genCode/sys_oper_log');
        xhr.responseType = 'blob';
        xhr.setRequestHeader('Authorization', getCookie("token"));
        //请求成功回调函数
        xhr.onload = function () {
            if (xhr.status === 200) {
                var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
                var contentDisposition = decodeURI(xhr.getResponseHeader("Content-Disposition"));
                var result = patt.exec(contentDisposition)
                var filename = result[1].replace(/\"/g, '')
                var response = xhr.response;
                //数据转换为文件下载
                var elink = document.createElement('a');
                elink.download = filename;
                elink.style.display = 'none';
                var blob = new Blob([response]);
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                document.body.removeChild(elink);
            }
        }
        xhr.send();

    })
    //导出定时任务
    $("#export-job").click(function () {
        $.ajax({
            url: "/system/job/export",
            type: "GET",
            dataType: "JSON",
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'Authorization': getCookie("token")
            },
            success: function (res) {
                if (res.status === 200) {
                    layer.msg('导出成功')
                } else {
                    layer.msg(res.msg)
                }
            }
        })
    })
    //插入一条定时任务
    $("#insert-job").click(function () {
        $.ajax({
            url: "/system/job/insertOneJob",
            type: "POST",
            dataType: "JSON",
            data: JSON.stringify(insertJobData),
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'Authorization': getCookie("token")
            },
            success: function (res) {
                console.log(res)
                if (res.status === 200) {
                    layer.msg('插入成功')
                } else {
                    layer.msg(res.msg)
                }
            }
        })
    })
    //改变定时任务状态
    $("#change-job1").click(function () {
        $.ajax({
            url: "/system/job/changeStatus",
            type: "POST",
            dataType: "JSON",
            data: JSON.stringify(changeJobData),
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'Authorization': getCookie("token")
            },
            success: function (res) {
                console.log(res)
                if (res.status === 200) {
                    layer.msg('修改成功。')
                } else {
                    layer.msg(res.msg)
                }
            }
        })
    })
    //获取定时任务列表
    $("#job").click(function () {
        $.ajax({
            url: "/system/job/list",
            type: "GET",
            dataType: "JSON",
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'Authorization': getCookie("token")
            },
            success: function (res) {
                console.log(res)
                if (res.status === 200) {
                    if (res.data != null && res.data.length > 0) {
                        //把jobId=1这条数据赋予给全局变量供测试使用
                        for (let i = 0; i < res.data.length; i++) {
                            if (res.data[i].jobId === 1) {
                                changeJobData = res.data[i]
                            }
                        }
                    }
                    $("#system-job").text(JSON.stringify(res.data))
                } else {
                    layer.msg(res.msg)
                }
            }
        })
    })
    //获取用户列表
    $("#user").click(function () {
        $.ajax({
            url: "/user/getAllUser",
            type: "GET",
            dataType: "JSON",
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'Authorization': getCookie("token")
            },
            success: function (res) {
                console.log(res)
                if (res.status === 200) {
                    let content = "";
                    for (let i = 0; i < res.data.length; i++) {
                        content += "<strong>id:" + res.data[i].userId + "_____用户名:" + res.data[i].userName + "____电话:" + res.data[i].phonenumber + "_____密码:" + res.data[i].password + "</strong></br>"
                    }
                    $("#user-list").html(content)
                } else {
                    alert(res.msg)
                }
            }
        })
    })
    //获取系统资源信息
    $("#system").click(function () {
        $.ajax({
            url: "/system/getSystemInfo",
            type: "GET",
            dataType: "JSON",
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'Authorization': getCookie("token")
            },
            success: function (res) {
                console.log(res)
                if (res.status === 200) {
                    $("#system-info").text(JSON.stringify(res.data))
                } else {
                    layer.msg(res.msg)
                }
            }
        })
    })
</script>
</body>
</html>